
package com.x52im.mall.logic.shop;

import android.app.Activity;
import android.widget.TextView;

import com.x52im.rbchat.R;
import com.x52im.mall.Utility;

/**
 * TODO 目前为了节省开发时间程序里的物流是写死的，以后要从数据库表sales_trsfer_for_country中按国家读取。
 * 
 * 订单确认：物流信息（包括物流费用）包装类.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public abstract class OrderComfirmTransferFareWrapper
{
	private Activity parentActivity = null;

	private __TransferFare transferFare = null;

	/** 标签组件：物流名称 */
	private TextView viewName = null;
	/** 标签组件：物流费用货币类型 */
	private TextView viewFareAmountCurrency = null;
	/** 标签组件：物流费用金额 */
	private TextView viewFareAmount = null;

	public OrderComfirmTransferFareWrapper(Activity parentActivity)
	{
		this.parentActivity = parentActivity;

		initViews();
		initListeners();
	}

	private void initViews()
	{
		viewName = (TextView)parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_transfer_nameView);
		viewFareAmountCurrency = (TextView)parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_transfer_fareAmountCurrencyView);
		viewFareAmount = (TextView)parentActivity.findViewById(R.id.common_mall_shop_layout_order_confirm_transfer_fareAmountView);
	}

	private void initListeners()
	{
	}

	/**
	 * 设置新的数据.
	 * 
	 * @param consigneeInfo
	 */
	public void setTransferFare(__TransferFare transferFare)
	{
		this.transferFare = transferFare;
		refreshTransferFare();
	}

	public __TransferFare getTransferFare()
	{
		return this.transferFare;
	}

	/**
	 * 用最新的数据来刷新UI.
	 */
	private void refreshTransferFare()
	{
		if(transferFare != null)
		{
			viewName.setText(transferFare.getTransferName());
			// 货币 类型目前使用默认值“美元”吧，数据库中美元货币类型的值是“0”
			viewFareAmountCurrency.setText(Utility.DEFAULT_CURRENCY_TYPE_NAME);
			viewFareAmount.setText(String.valueOf(transferFare.getFareAmount()));
			
			transferFareOnChaned(transferFare.getFareAmount());
		}
	}
	
	/**
	 * 运费更新时要调用的方法. 
	 * 
	 * @param fareAmount 新的运费金额
	 */
	protected abstract void transferFareOnChaned(double fareAmount);
	

	/**
	 * 数据合法性检验，保证要提交到服务端的数据不为空.
	 * 
	 * @return
	 */
	public boolean isValid()
	{
		if(transferFare != null)
		{
			//				String.valueOf(txtUid.getText()).trim().length()
			return __isValid(transferFare.getTransferId());
		}
		return false;
	}
	private boolean __isValid(String text)
	{
		return text != null && text.trim().length() > 0;
	}
	
	//-----------------------------------------------------------------------------------------
	static class __TransferFare
	{
		private String transferId = null;
		private String transferName = null;
		private double fareAmount = 0;

		public __TransferFare(String transferId, String transferName, double fareAmount)
		{
			this.transferId = transferId;
			this.transferName = transferName;
			this.fareAmount = fareAmount;
		}

		public String getTransferId()
		{
			return transferId;
		}
		public void setTransferId(String transferId)
		{
			this.transferId = transferId;
		}
		public String getTransferName()
		{
			return transferName;
		}
		public void setTransferName(String transferName)
		{
			this.transferName = transferName;
		}
		public double getFareAmount()
		{
			return fareAmount;
		}
		public void setFareAmount(double fareAmount)
		{
			this.fareAmount = fareAmount;
		}
	}
}